package seatechit.msbgateway.dbaccess.dao.impl;

import java.sql.ResultSet;

import seatechit.msbgateway.dbaccess.SpringFrameworkDAO;
import seatechit.msbgateway.dbaccess.dao.TransactionParamDAO;
import seatechit.msbgateway.dbaccess.entity.TransactionParam;
import seatechit.msbgateway.utils.Global;

public class TransactionParamDAOImpl extends SpringFrameworkDAO implements TransactionParamDAO {

	public TransactionParamDAOImpl() {
	}

	@Override
	public TransactionParam getTransactionParamByTranCode(String tranCode) {

		ResultSet rs = null;
		TransactionParam tParam = null;
		try {
			StringBuffer buffSQL = new StringBuffer();
			buffSQL.append(" SELECT a.tran_code, d.staff_name teller_id, e.staff_name approver_id, ");
			buffSQL.append(" c.channel_name, b.service_url, p1.p_value txstatus, p2.p_value txdate, ");
			buffSQL.append(" seq_tran_sn.nextval txnum,a.ref_host_tran_code,b.service_timeout,a.enabled_offline,a.sorn ");
			buffSQL.append(" FROM bec_trandef a,bec_srvdef b,bec_chldef c, ");
			buffSQL.append(" bec_staff d,bec_staff e,bec_param p1,bec_param p2 ");
			buffSQL.append(" WHERE a.service_id = b.service_id AND a.channel_id = c.channel_id AND a.teller_id = d.staff_id");
			buffSQL.append(" AND a.approver_id = e.staff_id AND p1.p_name = 'APP_TRAN_STATUS' AND p2.p_name = 'APP_TRAN_DATE'");
			buffSQL.append(" AND a.tran_code='" + tranCode + "'");
			rs = executeForQuery(buffSQL.toString());
			while (rs != null && rs.next()) {
				tParam = new TransactionParam(rs.getString("tran_code"), rs.getString("teller_id"), rs.getString("approver_id"), rs.getString("channel_name"), rs
						.getString("service_url"), rs.getString("txstatus"), rs.getString("txdate"), rs.getString("txnum"), rs.getString("ref_host_tran_code"), Integer.parseInt(rs
						.getString("service_timeout")), rs.getString("enabled_offline"), rs.getString("sorn"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tParam;
	}

	public String getTransactionSequence() {
		ResultSet rs = null;
		try {

			StringBuffer buffSQL = new StringBuffer();
			buffSQL.append("SELECT " + Global.SEQ_TRAN_SN + ".NEXTVAL FROM DUAL");
			rs = executeForQuery(buffSQL.toString());
			while (rs != null && rs.next()) {
				return rs.getString(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
}
